/**
 * 选择字段trigger
 *
 * Created by GUY on 2015/9/15.
 * @class BI.SelectTextTrigger
 * @extends BI.Trigger
 */
BI.SelectTextTrigger = BI.inherit(BI.Trigger, {

    _defaultConfig: function () {
        return BI.extend(BI.SelectTextTrigger.superclass._defaultConfig.apply(this, arguments), {
            baseCls: "bi-select-text-trigger",
            height: 24,
            allowClear: false,
            valueFormatter: BI.emptyFn,
            defaultText: "",
        });
    },

    _init: function () {
        BI.SelectTextTrigger.superclass._init.apply(this, arguments);
        var self = this, o = this.options;
        var text = this._digest(o.value, o.items);
        this.trigger = BI.createWidget({
            type: "bi.text_trigger",
            element: this,
            height: o.height,
            readonly: o.readonly,
            text: text,
            defaultText: o.defaultText,
            textHgap: o.textHgap,
            textVgap: o.textVgap,
            textLgap: o.textLgap,
            textRgap: o.textRgap,
            textTgap: o.textTgap,
            textBgap: o.textBgap,
            tipType: o.tipType,
            title: null,
            allowClear: o.allowClear,
            listeners: [
                {
                    eventName: BI.TextTrigger.EVENT_CLEAR,
                    action: function () {
                        self.setText("");
                        self.fireEvent(BI.SelectTextTrigger.EVENT_CLEAR);
                    }
                }
            ]
        });
    },

    _digest: function (val, items) {
        var o = this.options;

        val = BI.isArray(val) ? val.slice() : [val];

        var result = [];

        //  提升valueFormatter的优先级
        if (o.valueFormatter !== BI.emptyFn && BI.isFunction(o.valueFormatter)) {
            BI.each(val, function (index, v) {
                result.push(o.valueFormatter(v));
            });
            return result.join(",");
        }

        var formatItems = BI.Tree.transformToArrayFormat(items);
        BI.each(formatItems, function (i, item) {
            if (BI.contains(val, item.value) && !BI.contains(result, item.text || item.value)) {
                result.push(item.text || item.value);
                BI.remove(val, item.value);
            }
        });

        if (result.length > 0 && val.length === 0) {
            return result.join(",");
        } else {
            return BI.isFunction(o.text) ? o.text() : o.text;
        }
    },

    setText: function (text) {
        this.options.text = text;
        this.trigger.setText(text);
    },

    setValue: function (val) {
        var formatText = this._digest(val, this.options.items);
        this.trigger.setText(formatText);
    },

    setTipType: function (v) {
        this.options.tipType = v;
        this.trigger.setTipType(v);
    },

    getTextor: function () {
        return this.trigger.getTextor();
    },

    populate: function (items) {
        this.options.items = items;
    }
});

BI.SelectTextTrigger.EVENT_CLEAR = "EVENT_CLEAR";
BI.shortcut("bi.select_text_trigger", BI.SelectTextTrigger);
